package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.AnnouncementBean;
import com.attendance.www.hr.beans.AnnouncementPageBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IAnnouncementService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 公告查询
 * 
 * @author 李俊杰 2020-06-04
 *
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

	@Resource
	private IAnnouncementService announcementService;

	/**
	 * 新增公告
	 * 
	 * @param request
	 * @param announcementBean
	 * @return
	 */
	@RequestMapping(value = "/saveAnnouncement.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveAnnouncement(HttpServletRequest request, @RequestBody AnnouncementBean announcementBean) {
		if (announcementBean.getTitle() == null || announcementBean.getContent() == null
				|| announcementBean.getExpireDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		if (announcementService.saveAnnouncement(announcementBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00007");
	}

	/**
	 * 删除公告
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteAnnouncement.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteAnnouncement(HttpServletRequest request) {
		String id = request.getParameter("id");

		if (StringUtils.isBlank(id)) {
			throw new ResultFaultException("0_00001");
		}
		if (announcementService.deleteAnnouncement(id)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00009");
	}

	/**
	 * 修改公告
	 * 
	 * @param request
	 * @param announcementBean
	 * @return
	 */
	@RequestMapping(value = "/updateAnnouncement.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateAnnouncement(HttpServletRequest request, @RequestBody AnnouncementBean announcementBean) {
		if (announcementBean.getId() == null || announcementBean.getTitle() == null
				|| announcementBean.getContent() == null || announcementBean.getExpireDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		if (announcementService.updateAnnouncement(announcementBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00008");
	}

	/**
	 * 查询公告信息(管理员界面)
	 * 
	 * @param request
	 * @param announcementBean
	 * @return
	 */
	@RequestMapping(value = "/getAnnouncementList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAnnouncementList(HttpServletRequest request, @RequestBody AnnouncementBean announcementBean) {
		if (announcementBean.getPageCount() == null || announcementBean.getPageNo() == null) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo = announcementBean.getPageNo();
		int pageCount = announcementBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		announcementBean.setStartNum(startNum);
		AnnouncementPageBean announcementPageBean = new AnnouncementPageBean();
		announcementPageBean = announcementService.getAnnouncementList(announcementBean);
		return ResultMap.success(announcementPageBean);
	}

	/**
	 * ID查询公告
	 * 
	 * @param request
	 * @param announcementBean
	 * @return
	 */
	@RequestMapping(value = "/getAnnouncementById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAnnouncementById(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(announcementService.getAnnouncementById(Integer.parseInt(id)));
	}

	/**
	 * 停用公告
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/isDeactivateAnnouncement.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object isDeactivateAnnouncement(HttpServletRequest request) {
		String id = request.getParameter("id");
		String isDeactivate = request.getParameter("isDeactivate");

		if (StringUtils.isBlank(id) || StringUtils.isBlank(isDeactivate)) {
			throw new ResultFaultException("0_00001");
		}
		if (announcementService.isDeactivateAnnouncement(id, isDeactivate)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00018");
	}

	/**
	 * 查询公告信息(用户界面)
	 * 
	 * @param request
	 * @param announcementBean
	 * @return
	 */
	@RequestMapping(value = "/getAnnouncementListByExpireDate.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAnnouncementListByExpireDate() {
		return ResultMap.success(announcementService.getAnnouncementListByExpireDate());
	}
}
